package com;

import com.xiaoleilu.hutool.io.FileUtil;
import com.xiaoleilu.hutool.json.JSONArray;
import com.xiaoleilu.hutool.json.JSONObject;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.util.CollectionUtil;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class JsonUtil {

  public static void main(String[] args) {
    JSONObject jsonObject =
        JSONUtil.readJSONObject(FileUtil.file("D:/testjson.txt"), Charset.forName("UTF-8"));
    JSONArray arr = jsonObject.getJSONArray("dataset");
    if (CollectionUtil.isNotEmpty(arr)) {
      for (Object o1 : arr) {
        JSONArray subArr = JSONUtil.parseObj(o1).getJSONArray("bill");
        if (CollectionUtil.isEmpty(subArr)
            || !JSONUtil.parseObj(subArr.get(0)).containsKey("sort")) {
//          return jsonObject;
        }else{
          List list = subArr.stream().sorted(Comparator.comparing(obj -> ((JSONObject) obj).getInt("sort"))).collect(Collectors.toList());
          subArr.clear();
          list.forEach(obj-> subArr.put(obj));
        }
      }
      // return jsonObject;
    }

    System.out.println(jsonObject);
  }

  public static List<Person> sort(){
    Person p1 = new Person();
    p1.setAge(1);
    Person p2 = new Person();
    p2.setAge(12);
    Person p3 = new Person();
    p3.setAge(13);
    List<Person> list = new ArrayList<>();
    list.add(p2);
    list.add(p3);
    list.add(p1);
    System.out.println(list);

    List list2 = list.stream().sorted((o1, o2) -> o1.getAge().compareTo(o2.getAge())).collect(Collectors.toList());
    System.out.println(list2);
    return list2;
  }
}
